package com.sicnu.demo05

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    
    private lateinit var editText: EditText
    private lateinit var textView: TextView
    
    private val thirdActivityLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        if (result.resultCode == RESULT_OK) {
            val data = result.data?.getStringExtra("result")
            textView.text = getString(R.string.result) + " " + data
        }
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        
        editText = findViewById(R.id.editText)
        textView = findViewById(R.id.textView)
        val first = findViewById<Button>(R.id.first)
        val second = findViewById<Button>(R.id.second)
        val third = findViewById<Button>(R.id.third)
        val fourth = findViewById<Button>(R.id.fourth)
        
        first.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            val param = editText.text.toString()
            intent.putExtra("param", param)
            startActivity(intent)
        }
        
        second.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            thirdActivityLauncher.launch(intent)
        }
        
        third.setOnClickListener {
            val intent = Intent(this, CameraActivity::class.java)
            startActivity(intent)
        }
        
        fourth.setOnClickListener {
            val intent = Intent(this, StopWatchActivity::class.java)
            startActivity(intent)
        }
    }
}